package com.colin.app.activity.other.recycler;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

/**
 * 2.定义一个抽象类，用于继承RecyclerView.OnScrollListener并实现我们定义的接口OnLoadMoreListener，
 * 如果需要上拉加载更多，直接为RecyclerView添加滚动监听为我们的实现类即可
 * <p>
 * 由于RecyclerView默认有三种布局，所以我们要对这三种布局分别进行判断上拉加载，处理的逻辑有点不同，首先添加如下定义
 */

public abstract class OnRecyclerViewScrollListener<T> extends RecyclerView.OnScrollListener implements OnLoadMoreListener<T> {

    public final static int MANAGER_TYPE_LINEAR = 0;
    public final static int MANAGER_TYPE_GRID = 1;
    public final static int MANAGER_TYPE_STAGGERED = 2;


    protected int layoutManagerType = MANAGER_TYPE_LINEAR;

    private boolean loadMore = false;


    private int[] lastPositions;
    private int lastVisibleItemPosition;
    private int currentScrollState = 0;


    public boolean isLoadMore() {
        return loadMore;
    }

    public void setLoadMore(boolean loadMore) {
        this.loadMore = loadMore;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            layoutManagerType = MANAGER_TYPE_GRID;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            layoutManagerType = MANAGER_TYPE_STAGGERED;
        } else if (layoutManager instanceof LinearLayoutManager) {
            layoutManagerType = MANAGER_TYPE_LINEAR;
        } else {
            throw new RuntimeException("Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
        }

        switch (layoutManagerType) {
            case MANAGER_TYPE_LINEAR:
                lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
                break;
            case MANAGER_TYPE_GRID:
                lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
                break;
            case MANAGER_TYPE_STAGGERED:
                StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
                if (lastPositions == null) {
                    lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
                }
                staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
                lastVisibleItemPosition = findMax(lastPositions);
                break;
            default:
                break;
        }
    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        currentScrollState = newState;
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        int visibleItemCount = layoutManager.getChildCount();
        int totalItemCount = layoutManager.getItemCount();
        if (visibleItemCount > 0 && currentScrollState == RecyclerView.SCROLL_STATE_IDLE
                && lastVisibleItemPosition >= totalItemCount - 1) {
            if (!isLoadMore()) {
                loadMore = true;
                onStart();
                onLoadMore();
            }
        }
    }

    private int findMax(int[] lastPositions) {
        int max = lastPositions[0];
        for (int value : lastPositions) {
            if (value > max) {
                max = value;
            }
        }
        return max;
    }
}
